package com.jeecms.wicool.manager.car.impl;



import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.hibernate3.Updater;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.car.CarTypeDao;
import com.jeecms.wicool.entity.car.CarBrand;
import com.jeecms.wicool.entity.car.CarSeries;
import com.jeecms.wicool.entity.car.CarType;
import com.jeecms.wicool.entity.shops.ShopInfo;
import com.jeecms.wicool.manager.car.CarBrandMng;
import com.jeecms.wicool.manager.car.CarSeriesMng;
import com.jeecms.wicool.manager.car.CarTypeMng;
import com.jeecms.wicool.manager.shops.WiShopInfoMng;

@Service
@Transactional
public class CarTypeMngImpl implements CarTypeMng {
	
	private CarTypeDao dao;
	@Autowired
	public void setDao(CarTypeDao dao) {
		this.dao = dao;
	}
	
	@Transactional(readOnly = true)
	public Pagination getPage(String typeName,Integer carBrandId,Integer carSeriesId,Integer year ,int pageNo, int pageSize,CmsUser user) {
		Pagination page = dao.getPage(typeName, carBrandId, carSeriesId, year,pageNo, pageSize,user);
		return page;
	}
	@Override
	public CarType findById(Integer id) {
		CarType entity = dao.findById(id);
		return entity;
	}

	@Override
	public CarType update(CarType bean,Integer shopId,Integer carBrandId,Integer carSeriesId) {
		ShopInfo shop = shopMng.findById(shopId);
		CarBrand carBrand=carBrandMng.findById(carBrandId);
		CarSeries carSeries=carSeriesMng.findById(carSeriesId);
		bean.setShop(shop);
		bean.setCarBrand(carBrand);
		bean.setCarSeries(carSeries);
		Updater<CarType> updater = new Updater<CarType>(bean);
		bean = dao.updateByUpdater(updater);
		return bean;
	}

	@Override
	public CarType save(CarType bean,Integer shopId,Integer carBrandId,Integer carSeriesId) {
		ShopInfo shop = shopMng.findById(shopId);
		CarBrand carBrand=carBrandMng.findById(carBrandId);
		CarSeries carSeries=carSeriesMng.findById(carSeriesId);
		bean.setShop(shop);
		bean.setCarBrand(carBrand);
		bean.setCarSeries(carSeries);
		dao.save(bean);
		return bean;
	}

	@Override
	public CarType deleteById(Integer id) {
		CarType bean = dao.deleteById(id);
		return bean;
	}
	@Override
	public CarType[] deleteByIds(Integer[] ids) {
		CarType[] beans = new CarType[ids.length];
		for (int i = 0,len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}
	@Override
	public List<CarType> getAll(CmsUser user) {
		return dao.getAll(user);
	}
	@Override
	public List<CarType> getCarTypeByCarSeries(Integer carSeriesId,CmsUser user){
		return dao.getCarTypeByCarSeries(carSeriesId,user);
	}
	@Autowired
	private WiShopInfoMng shopMng;
	@Autowired
	private CarBrandMng carBrandMng;
	@Autowired
	private CarSeriesMng carSeriesMng;
	
}